#include <QtGui>
#include "configdlg.h"
#include "valueform.h"
#include "../core/aprcfg.h"
#include "../core/measprog.h"


ConfigDlg::ConfigDlg(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);
	okButton      = ui.okButton;
	cancelButton  = ui.cancelButton;
	endTimeButton = ui.endtimeButton;
	offTimeButton = ui.offtimeButton;
	ngButton      = ui.ngButton;
	hdrLogicButton= ui.hdrLogicButton;
	dlmButton     = ui.dlmButton;
	filterCombo   = ui.filterCombo;
	selstepCheck  = ui.selstepCheck;

	QStringList filter_opts;
	AprCfg::getNamesByAttr(PM_FILFREQ_ATTR, filter_opts);
	filterCombo->addItems(filter_opts);

	loadConfig();

	// set font size
	QFont font = this->font();
	font.setPixelSize(font.pointSize() * 4 / 3);
	setFont(font);

	setWindowFlags(Qt::CustomizeWindowHint | Qt::FramelessWindowHint);
	showMaximized();

	connect(endTimeButton, SIGNAL(clicked()), this, SLOT(editingValue()));
	connect(offTimeButton, SIGNAL(clicked()), this, SLOT(editingValue()));
	connect(hdrLogicButton, SIGNAL(clicked()), this, SLOT(editingValue()));
	connect(ngButton, SIGNAL(clicked()), this, SLOT(editingValue()));
	connect(dlmButton, SIGNAL(clicked()), this, SLOT(editingValue()));
	connect(okButton, SIGNAL(clicked()), this, SLOT(saveConfig()));
	connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
}


ConfigDlg::~ConfigDlg()
{

}

void ConfigDlg::editingValue()
{
	QPushButton *button = qobject_cast<QPushButton *>(sender());
	ushort range_code = 0;
	const rng_tbl_s *range = NULL;
	if(button == endTimeButton){
		range_code = PM_T99_9ms;
	}else if(button == offTimeButton){
		range_code = PM_T99_9ms;
	}else if(button == ngButton){
		range_code = PM_COUNT;
	}else if(button == hdrLogicButton){
		range_code = PM_LOGIC;
	}else if(button == dlmButton){
		range_code = PM_DLMADJ;
	}
	range = AprCfg::getRangeByCode(range_code);
	ValueForm *dlg = new ValueForm(button->text().toDouble(), range, this);
	if(QDialog::Accepted == dlg->exec()){
		button->setText(dlg->getValueString());
	}
	delete dlg;
}

void ConfigDlg::loadConfig()
{
	MeasProg* pMeasProg = MeasProg::getInstance();
	program_s* p = pMeasProg->getProgPtr();

	// filter freq
	QString filter_str;
	AprCfg::getParamNameByCode(p->filfreq, filter_str);
	int freq_index = filterCombo->findText(filter_str);
	filterCombo->setCurrentIndex(freq_index);

	// auto meas
//	ckbAutoSeq->setChecked((bool)p->autoseq);
//	ckbAutoSeq->setChecked(PM_ON == p->autoseq);

	// warn ng count
	ngButton->setText(QString::number(p->warn_ngcount));

	// hdr logic
	hdrLogicButton->setText(QString::number(p->hdr_logic));

	// end time
	endTimeButton->setText(QString::number(p->endtime));

	// off time
	offTimeButton->setText(QString::number(p->offtime));

	// selstep
	selstepCheck->setChecked(PM_ON == p->selstep);

	// ng count on
//	ckbNgCntOn->setChecked(PM_ON == p->autoseq);

	// tone vol
//	sliToneVol->setValue(p->tonevolume);
	
	// alt vol
//	sliAltVol->setValue(p->altvolume);

	// dlm adj
	dlmButton->setText(QString::number(p->dlm_k));
}




void ConfigDlg::saveConfig()
{
	MeasProg* pMeasProg = MeasProg::getInstance();
	program_s* p = pMeasProg->getProgPtr();

	// filter freq
	QString filter_str = filterCombo->currentText();
	ushort filfreq_code = 0;
	AprCfg::getParamCodeByName(filter_str, PM_FILFREQ_ATTR, filfreq_code);
	p->filfreq = filfreq_code;

	// auto meas
//	p->autoseq = (short)ckbAutoSeq->isChecked();
//	p->autoseq = ckbAutoSeq->isChecked() ? PM_ON : PM_OFF;

	// warn ng count
	p->warn_ngcount = ngButton->text().toInt();

	// hdr logic
	p->hdr_logic = hdrLogicButton->text().toInt();

	// end time
	p->endtime = endTimeButton->text().toDouble();

	// off time
	p->offtime = offTimeButton->text().toDouble();

	// sel step 
	p->selstep = selstepCheck->isChecked() ? PM_ON : PM_OFF; 

	// ng count on
//	p->ngcounton = (short)ckbNgCntOn->isChecked();
//	p->ngcounton = ckbNgCntOn->isChecked() ? PM_ON : PM_OFF;

	// tone vol
//	p->tonevolume = sliToneVol->value();

	// alt vol
//	p->altvolume = sliAltVol->value();

	// dlm adj
	p->dlm_k = dlmButton->text().toInt();

	accept();
}
